package com.anycc.cloud.common.dto.easyui;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;

import java.io.Serializable;
import java.util.*;

/**
 * easyui tree data structure
 */
public class Tree implements Serializable {
    private static final long serialVersionUID = 6990023079740384714L;
    public static final String STATE_CLOSED = "closed";
    public static final String STATE_OPEN = "open";

    private String id;
    private String text;
    private String state;
    private Boolean checked;
    private Map<String, Object> attributes = new HashMap();
    private List<Tree> children = new ArrayList();
    private String iconCls;

    public Tree() {
    }

    public Tree(String id, String text) {
        this.id = id;
        this.text = text;
    }

    public Tree(String id, String text, String iconCls) {
        this.id = id;
        this.text = text;
        this.iconCls = iconCls;
    }

    @JsonProperty("id")
    public String getId() {
        return this.id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @JsonProperty("text")
    public String getText() {
        return this.text;
    }

    public void setText(String text) {
        this.text = text;
    }

    @JsonIgnore
    private boolean isChildChecked(List<Tree> children) {
        boolean checkedChild = false;
        for (Tree child : children) {
            if (child.getChecked() != null && child.getChecked() || !child.getChildren().isEmpty() && this.isChildChecked(child.getChildren())) {
                checkedChild = true;
                break;
            }
        }
        return checkedChild;
    }

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("state")
    public String getState() {
        if (StringUtils.equalsAny(this.state, "open", "closed")) {
            return this.state;
        } else {
            if (!this.children.isEmpty() && !this.isChildChecked(this.children)) {
                this.state = "closed";
            }

            return this.state;
        }
    }

    public void setState(String state) {
        this.state = state;
    }

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("checked")
    public Boolean getChecked() {
        return this.checked;
    }

    public void setChecked(Boolean checked) {
        this.checked = checked;
    }

    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    public Map<String, Object> getAttributes() {
        return this.attributes;
    }

    public void setAttributes(Map<String, Object> attributes) {
        this.attributes = attributes;
    }

    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    public List<Tree> getChildren() {
        return this.children;
    }

    public void setChildren(List<Tree> children) {
        this.children = children;
    }

    @JsonInclude(JsonInclude.Include.NON_NULL)
    public String getIconCls() {
        return this.iconCls;
    }

    public void setIconCls(String iconCls) {
        this.iconCls = iconCls;
    }

    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}


